package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.ProductBrandDao;
import com.ytjj.qmyx.admin.dao.SpecialDao;
import com.ytjj.qmyx.admin.model.CountryInfo;
import com.ytjj.qmyx.admin.model.request.ProductBrandRequest;
import com.ytjj.qmyx.admin.model.response.BrandChooseResponse;
import com.ytjj.qmyx.admin.model.response.ProductBrandResponse;
import com.ytjj.qmyx.admin.service.ProductBrandService;
import com.ytjj.qmyx.mall.model.ProductBrand;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 
 * @Version: 1.0
 */
@Service
public class ProductBrandServiceImpl implements ProductBrandService {

	@Resource
	private ProductBrandDao productBrandDao;

	@Resource
	private SpecialDao specialDao;

	@Override
	public List<BrandChooseResponse> getProductBrandList() {
		return productBrandDao.getProductBrandList();
	}

	@Override
	public CommonPage<ProductBrandResponse> selectList(ProductBrandRequest request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if (request.getSpecialId() != null) {
			request.setSpecial(request.getSpecialId().toString());
		}
		List<ProductBrandResponse> pageList = productBrandDao.selectList(request);
		if (!CollectionUtils.isEmpty(pageList)) {
			for (ProductBrandResponse response : pageList) {
				 if(response.getSpecialId() != null) {
					 String specialName = "";
					 String[] specialId = response.getSpecialId().split(",");
					 for(String sid : specialId) {
						 specialName = specialName + specialDao.getSpecialName(Integer.valueOf(sid)) + " ";
					 }
					 response.setSpecialName(specialName);
				 }
			}
		}
		return CommonPage.restPage(pageList);
	}

	@Override
	public Integer addProductBrand(ProductBrand productBrand) {
		if (productBrandDao.isExists(productBrand.getBrandName()) > 0) {
			throw new ApiException("该品牌已存在，请勿重复添加！");
		}
		return productBrandDao.addProductBrand(productBrand);
	}

	@Override
	public Integer udpateProductBrand(ProductBrand productBrand) {
		return productBrandDao.udpateProductBrand(productBrand);
	}

	@Override
	public Integer deleteProductBrand(Integer id) {
		return productBrandDao.deleteProductBrand(id);
	}

	@Override
	public List<CountryInfo> findCountryAll() {
		return productBrandDao.findCountryAll();
	}
}
